class Settlement {
    constructor() {
        // 从地址总寻找信息
        this.getInf();
        //设置留言看板
        this.$('.leaveMessageImg').onclick = () => {
            this.$('.leaveMessageImg').style.display = 'none';
            this.$('.leaveMessageTextareaSpan').style.display = 'block';
            this.$('.orderItemSumDiv').style.height = '100px';
        }
        //收货地址的准备
        this.$('.submitOrderButton').addEventListener('click',this.goodsSpace)
    }
    //从地址中寻找信息的方法
    async getInf() {
        let inf = location.search.split('&');
        inf[0] = inf[0].split('?').pop();
        inf = inf.slice(0, -1);
        // console.log(inf);
        // 发送请求购物车列表
        // 获取locastorage中的id和token
        let id = localStorage.getItem('id');
        const AUTH_TOKEN = localStorage.getItem('token');
        axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
        // console.log(id);
        //发送请求购物车列表
        let res = await axios.get('http://localhost:8888/cart/list?id=' + id);
        // console.log(res);
        if (res.data.code == 0 && res.status == 200) {
            location.assign('login.html?comeFrom=Settlement.html')
        }
        if (res.data.code == 401 && res.status == 200) {
            location.assign('login.html?comeFrom=Settlement.html')
        }
        let ele = res.data.cart;
        // console.log(ele);
        // 准备往页面中插入的内容
        let str = '';
        //准备总价的计算
        let sum=0;
        //遍历购物车中所有的内容
        ele.forEach(element => {
            //如果其货号和购物车传递过来的一致,就采用
            if (inf.includes(element.goods_id + '')) {
                //    console.log(11111);
                //字符串拼接
                str += `
                <tr class="orderItemTR">
							<td class="orderItemFirstTD"><img class="orderItemImg"
							src=${element.img_small_logo}>
							</td>
							<td class="orderItemProductInfo">
								<a class="orderItemProductLink" href="#nowhere">
									${element.title}
								</a>
								<img src="http://how2j.cn/tmall/img/site/creditcard.png" title="支持信用卡支付">
								<img src="http://how2j.cn/tmall/img/site/7day.png" title="消费者保障服务,承诺7天退货">
								<img src="http://how2j.cn/tmall/img/site/promise.png" title="消费者保障服务,承诺如实描述">
							</td>
							<td>
								<span class="orderItemProductPrice">￥${element.current_price}</span>
							</td>
							<td>
								<span class="orderItemProductNumber">${element.cart_number}</span>
							</td>
							<td><span class="orderItemUnitSum">
						￥${(element.current_price * element.cart_number).toFixed(2)}
						</span></td>
							
						</tr>
                `
                // console.log(str);
                //获取页面中的插入点
                this.$('.productListTableTbody').innerHTML = str;
                //总价计算累加
                sum+=((element.current_price) * element.cart_number).toFixed(2)-0;
                // console.log(sum);
                // console.log(typeof(sum));
                //设置总价,获取需要插入总价的节点
                this.$('.orderItemTotalSumSpan').innerHTML='￥'+sum;
                this.$('.orderItemSumDiv .pull-right').innerHTML='店铺合计(含运费): ￥'+sum;
                // 设置总价为Settlement类的一个属性,便于调用
                Settlement.sum=sum;
            }
        })

    }
    //收货地址的准备
    goodsSpace=()=>{
        //分别获取输入值
        let useaddress= this.$('.useddress').value.trim();
        let usepost= this.$('.usepost').value.trim();
        let usename= this.$('.usename').value.trim();
        let usemobile= this.$('.usemobile').value.trim();
        //判断如果有一项为空就进行警告
        if(!useaddress||!usepost||!usename||!usemobile){

            alert('请详细检查您的收货地址,不能为空')
            return;
        }else{
            //地址栏放不下,放进localstorage中
            localStorage.setItem('useaddress',useaddress);
            localStorage.setItem('usepost',usepost);
            localStorage.setItem('usename',usename);
            localStorage.setItem('usemobile',usemobile);
            // console.log(Settlement.sum);
            location.assign('pay.html?'+Settlement.sum)
        }
    }
    /* *****获取节点的方法****** */
    $(ele) {
        let node = document.querySelectorAll(ele)
        return node.length == 1 ? node[0] : node;
    }
}
new Settlement;
